package com.zkthink.ceres.admin.dashboard.controller;

import cn.hutool.core.util.NumberUtil;
import com.google.common.collect.Maps;
import com.zkthink.base.R;
import com.zkthink.cache.repository.RedisRepositoryImpl;
import com.zkthink.ceres.common.enums.StoreDailyStatisticEnum;
import com.zkthink.ceres.order.service.OrderProductService;
import com.zkthink.ceres.order.service.OrderService;
import com.zkthink.ceres.store.entity.Store;
import com.zkthink.ceres.store.entity.StoreDailyStatistic;
import com.zkthink.ceres.store.service.StoreDailyStatisticService;
import com.zkthink.ceres.store.service.StoreService;
import com.zkthink.context.BaseContextHandler;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

import static com.zkthink.ceres.common.constant.CacheKey.buildStorePointKey;

/**
 * Created by kellen on 2020/5/30.
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/dashBoard")
@Api(value = "dashBoard", tags = "dashBoard")
@AllArgsConstructor
public class StoreDashboardController {

  private OrderService orderService;

  private RedisRepositoryImpl redisRepository;

  private OrderProductService orderProductService;

  private StoreService storeService;

  private StoreDailyStatisticService storeDailyStatisticService;


  @ApiOperation(value = "商家概览", notes = "商家概览")
  @ApiImplicitParams({
          @ApiImplicitParam(name = "offsetDay", value = "查询间隔日期，当天传0，昨天传1，最近7天传7，最近30天传30，上个月的传60", type = "int", required = true, defaultValue = "0")
  })
  @GetMapping("/store")
  public R store(@RequestParam(name = "offsetDay", defaultValue = "0") Integer offsetDay) {
    String tenantCode = BaseContextHandler.getTenant();
    Store store = storeService.findByTenantCode(tenantCode);
    Integer storeVisitCount = redisRepository
            .get(buildStorePointKey(StoreDailyStatisticEnum.PV, store.getId()));
    Map<String, Object> param = Maps.newHashMap();
    param.put("offsetDay", offsetDay);
    param.put("tenantCode", tenantCode);

    Map<String, Object> countMap = orderService.countTodayOrder(param);
    List<Map<String, Object>> saleProductList = orderProductService.countProductSale(param);
    countMap.put("storeVisitCount", storeVisitCount);
    countMap.put("saleProductList", saleProductList);
    List<StoreDailyStatistic> storeDailyStatistics = storeDailyStatisticService
            .countStoreDailyData(param);
    //构建订单转化漏斗数据
    long sumPV = storeDailyStatistics.stream().mapToLong(StoreDailyStatistic::getPv).sum();
    long sumAddCartCount = storeDailyStatistics.stream()
            .mapToLong(StoreDailyStatistic::getAddCartCount).sum();
    long sumSettleCount = storeDailyStatistics.stream()
            .mapToLong(StoreDailyStatistic::getSettleCount).sum();
    long sumPayCount = storeDailyStatistics.stream().mapToLong(StoreDailyStatistic::getPayCount)
            .sum();
    long sumPaySuccessCount = storeDailyStatistics.stream()
            .mapToLong(StoreDailyStatistic::getPaySuccessCount).sum();
    Map<String, Object> changeData = Maps.newHashMap();
    //访问次数
    changeData.put("sumPV", sumPV);
    //加入购物车次数
    changeData.put("sumAddCartCount", sumAddCartCount);
    //结算次数
    changeData.put("sumSettleCount", sumSettleCount);
    //支付次数
    changeData.put("sumPayCount", sumPayCount);
    //支付成功次数
    changeData.put("sumPaySuccessCount", sumPaySuccessCount);
    //总转化率
    changeData.put("totalChangeRate",
            (0 == sumPaySuccessCount || sumPV == 0) ? 0 : NumberUtil.div(sumPaySuccessCount, sumPV, 2) * 100);
    //加入购物车转化率
    changeData.put("addCartChangeRate",
            (0 == sumAddCartCount || sumPV == 0) ? 0 : NumberUtil.div(sumAddCartCount, sumPV, 2) * 100);
    //结账转化率
    changeData
            .put("settleChangeRate", (0 == sumSettleCount || sumAddCartCount == 0) ? 0 : NumberUtil.div(sumSettleCount, sumAddCartCount, 2) * 100);
    //调用支付转化率
    changeData
            .put("payCountChangeRate", (0 == sumPayCount || sumSettleCount == 0) ? 0 : NumberUtil.div(sumPayCount, sumSettleCount, 2) * 100);
    //支付成功转化率
    changeData
            .put("paySuccessChangeRate", (0 == sumPaySuccessCount || 0 == sumPayCount) ? 0 : NumberUtil.div(sumPaySuccessCount, sumPayCount, 2) * 100);
    countMap.put("changeData", changeData);
    countMap.put("storeDailyStatistics", storeDailyStatistics);
    return R.success(countMap);
  }

  @ApiOperation(value = "商家概览2", notes = "商家概览2")
  @GetMapping("/storeAll")
  public R storeAll() {
    String tenantCode = BaseContextHandler.getTenant();
    Store store = storeService.findByTenantCode(tenantCode);
    Map map = orderService.storeAll(store);
    return R.success(map);
  }
}
